//
//  LZFLabel.m
//  LZFMenu
//
//  Created by 梁梓烽 on 16/7/18.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFLabel.h"

@implementation LZFLabel
-(void)awakeFromNib
{
 
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel)]];
    self.userInteractionEnabled=YES;

}
-(void)clickLabel
{
    [self becomeFirstResponder];
    UIMenuController *menu=[UIMenuController sharedMenuController];
    
    //自定义menuitem
    menu.menuItems=@[
                     [[UIMenuItem alloc]initWithTitle:@"顶" action:@selector(ding)],
                     
                     [[UIMenuItem alloc]initWithTitle:@"踩" action:@selector(cai)],
                     
                     [[UIMenuItem alloc]initWithTitle:@"回复" action:@selector(comment)]
                     
                     ];
    [menu setTargetRect:self.bounds inView:self];
    [menu setMenuVisible:YES animated:YES];
  
    //menu要弹出的话,要让第一响应者实现canPerformAction来告诉我们显示什么
}

//重写这个方法,表示该对象可以成为第一响应者
-(BOOL)canBecomeFirstResponder
{
    return YES;
}


//告诉我们menu显示什么内容
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if ((action==@selector(copy:)&&self.text!=nil)||
        action==@selector(paste:)||
        (action==@selector(cut:)&&self.text!=nil)||
        action==@selector(ding)||
         action==@selector(cai)||
         action==@selector(comment)
        ) {
        return YES;
    }
    
    return NO;
}

#pragma mark menu项的响应方法

-(void)ding
{
    NSLog(@"ding");
}

-(void)cai
{
    NSLog(@"cai");
}

-(void)comment
{
    NSLog(@"comment");
}

-(void)cut:(UIMenuController *)menu
{
    [UIPasteboard generalPasteboard].string=self.text;
    self.text=nil;
}

-(void)copy:(UIMenuController *)menu
{
     [UIPasteboard generalPasteboard].string=self.text;
}


-(void)paste:(UIMenuController *)menu
{
    self.text=[UIPasteboard generalPasteboard].string;
}
@end
